package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import view.items.ButtonNewCall;

import network.TCPServer;

import model.Call;
import model.EnumCallStatus;
import model.Model;
import controller.Controller;
import controller.ControllerLighter;



public class View extends JFrame {

	private Controller controller;
	private ControllerLighter controllerLighter;
	private ViewDialer viewDialer;
	private ViewCallRequest viewCallRequest;
	private ViewPendingCall viewPendingCall;
	private ViewInformation viewInformation;
	private ViewActiveCall viewActiveCall;
	private ButtonNewCall buttonNewCall;
	private ViewBottom viewBottom;
	private ViewSoundSetting viewSoundSetting;
	
	public View(Controller controller,ControllerLighter controllerLighter){
		this.controller = controller;
		this.controllerLighter = controllerLighter;
		 initView();
		 this.setContentPane(popView());
		 this.setVisible(true);
	}


	private void initView() {
		this.setTitle("AudioChat");
        this.setSize(980, 800);
         //this.setSize(100,100);
         this.setLocationRelativeTo(null);
         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         this.setResizable(false);
         
         //menu
         JMenuBar menu = new JMenuBar();
         JMenu file = new JMenu("File");
         JMenuItem exit = new JMenuItem("Exit");
         menu.add(file);
         file.add(exit);
         exit.addActionListener(controller);
         exit.setActionCommand("menuExit");
         
         this.setJMenuBar(menu);
	}


	private JPanel popView(){
		JPanel content = new JPanel();	
		content.setLayout(null);
			
		viewInformation = new ViewInformation(this.controller,20,20,300,150);
		content.add(viewInformation);
		
		buttonNewCall = new ButtonNewCall("New Call",340,20,300,30);
		buttonNewCall.addActionListener(this.controller);
		content.add(buttonNewCall);
		
		viewSoundSetting = new ViewSoundSetting(controller, 640, 50, 200, 120);
		content.add(viewSoundSetting);
		
		viewDialer = new ViewDialer(this.controller,340,50,300,120);
		content.add(viewDialer);
		
		
		
		viewCallRequest = new ViewCallRequest(this.controller, this.controllerLighter,20,190,300,250);
		content.add(viewCallRequest);	
		viewPendingCall = new ViewPendingCall(this.controller,this.controllerLighter,660,190,300,250);
		content.add(viewPendingCall);	
		viewActiveCall = new ViewActiveCall(this.controller,this.controllerLighter,340, 190, 300,250);
		content.add(viewActiveCall);
		
		viewBottom = new ViewBottom(this.controller,20, 460, 940,250);
		content.add(viewBottom);
		


		

		return content;

	}
	
	public void refreshAll(){
		System.out.println("refreshall");
		this.viewDialer.refresh();
		this.viewCallRequest.refresh();
		this.viewPendingCall.refresh();
		this.viewInformation.refresh();	
		this.viewActiveCall.refresh();
		this.viewBottom.refresh();
		this.viewSoundSetting.refresh();
		this.refresh();
		this.validate();
	}


	public void refresh() {
		//System.out.println("refreshView");
		if (this.controller.getModel().getShowDialer()) this.buttonNewCall.setEnabled(false);
		else this.buttonNewCall.setEnabled(true);
		this.validate();
	}
	
	
	
	
	public ViewDialer getViewDialer() {
		return viewDialer;
	}


	public ViewBottom getInfozone() {
		return viewBottom;
	}


	public void setInfozone(ViewBottom infozone) {
		this.viewBottom = infozone;
	}
	
	public ViewCallRequest getViewCallRequest() {
		return viewCallRequest;
	}


	public void setViewCallRequest(ViewCallRequest viewCallRequest) {
		this.viewCallRequest = viewCallRequest;
	}


	public ViewPendingCall getViewPendingCall() {
		return viewPendingCall;
	}


	public ViewInformation getViewInformation() {
		return viewInformation;
	}


	public ViewActiveCall getViewActiveCall() {
		return viewActiveCall;
	}


	public ViewBottom getViewBottom() {
		return viewBottom;
	}


	public ViewSoundSetting getViewSoundSetting() {
		return viewSoundSetting;
	}


	public Controller getController() {
		return controller;
	}


	
}
